﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MediatR;
using Ecommerce.Domain.Rabbit;
using EcommerceRabbit.Repository.Impl;
using EcommerceRabbit.API.Application.Command.Trace;
using Ecommerce.Domain.Dto;
using EcommerceRabbit.Repository.Interface;

namespace EcommerceRabbit.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TraceController : ControllerBase
    {
        private readonly IMediator mediator;

        public TraceController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        [HttpPost]
        public async Task<List<Traceability>> GetTraceabilities(TraceGetCommand command)
        {
            return await mediator.Send(command);
        }
        [HttpPost]
        public IActionResult Flie(IFormFile file)
        {
            var fileName = file.FileName;
            fileName = Guid.NewGuid() + Path.GetExtension(fileName);
            var path = Directory.GetCurrentDirectory() + @"\wwwroot\Image\" + fileName;
            using(FileStream fs = new FileStream(path,FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            var url = "/Image/" + fileName;
            return Ok(url);
        }
        [HttpPost]
        public async Task<int> AddTrace(TraceAddCommand command)
        {
            return await mediator.Send(command);
        }
        [HttpPost]
        public async Task<List<TraceDTO>> GetTraceDTO(TraceFanCommand command)
        {
            return await mediator.Send(command);
        }
        [HttpPost]
        public async Task<int> UpdateTrace(TraceUpdateCommand command)
        {
            return await mediator.Send(command);
        }
        [HttpPost]
        public async Task<List<Cultivation>> GetCultivations(CultiveCommand command)
        {
            return await mediator.Send(command);
        }
        [HttpPost]
        public async Task<List<Immune>> GetImmunes(ImmunesGetCommand command)
        {
            return await mediator.Send(command);
        }
        [HttpPost]
        public async Task<int> AddImmunes(ImmunesAddCommand command)
        {
            return await mediator.Send(command);
        }
        [HttpPost]
        public async Task<List<Enterprise>> GetEnterprises(EnterGetCommand command)
        {
            return await mediator.Send(command);
        }
    }
}
